package com.qumitech.visualnovel.model.dto.updatecfg;

import com.alibaba.fastjson.annotation.JSONField;
import com.qumitech.visualnovel.model.dao.app.BookUpdateCfgDO;
import com.qumitech.visualnovel.service.updatecfg.constant.BookUpdateCfgStatusEnum;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.util.Date;

@Data
public class BookUpdateCfgDTO {
    private Integer id;

    @JSONField(name = "book_id")
    private Integer bookId;

    @JSONField(name = "book_title")
    private String bookTitle;

    @JSONField(name = "cfg_type")
    private Integer cfgType;

    @JSONField(name = "publish_date")
    private Date publishDate;

    @JSONField(name = "order_index")
    private Integer orderIndex;

    private Integer status;

    @JSONField(name = "start_effect_time")
    private Date startEffectTime;

    @JSONField(name = "end_effect_time")
    private Date endEffectTime;

    public BookUpdateCfgDTO() {
    }

    public BookUpdateCfgDTO(BookUpdateCfgDO cfgDO) {
        BeanUtils.copyProperties(cfgDO, this);
    }

    public BookUpdateCfgDO convertDO() {
        BookUpdateCfgDO cfgDO = new BookUpdateCfgDO();
        BeanUtils.copyProperties(this, cfgDO);
        Date now = new Date();
        if(startEffectTime.after(now)){
            cfgDO.setStatus(BookUpdateCfgStatusEnum.WAIT.getStatus());
        }
        if(endEffectTime.before(now)){
            cfgDO.setStatus(BookUpdateCfgStatusEnum.EXPIRE.getStatus());
        }
        if(startEffectTime.before(now) && endEffectTime.after(now)){
            cfgDO.setStatus(BookUpdateCfgStatusEnum.ING.getStatus());
        }
        return cfgDO;
    }
}
